<template>
    <div class="article-search-container">
        <!-- 背景点缀 -->
        <div class="bg-container">
            <div class="bg-dot"></div>
            <div class="bg-dot dot-2"></div>
            <div class="bg-dot dot-3"></div>
        </div>

        <h1 class="section-title">文章搜索结果</h1>
        <div v-if="loading">加载中...</div>
        <div v-else>
            <div class="articles-container">
                <div
                    class="article-card"
                    v-for="article in articles"
                    :key="article.id"
                    @mouseenter="scaleCard(article.id)"
                    @mouseleave="resetScale(article.id)"
                    :style="{ '--scale': cardScale[article.id] || 1 }"
                >
                    <div class="article-image-container">
                        <img
                            :src="
                                article.coverImage
                                    ? `data:image/jpeg;base64,${article.coverImage}`
                                    : 'https://picsum.photos/400/200'
                            "
                            alt="文章封面"
                            class="article-image"
                        />
                        <div class="article-badge" v-if="article.isNew">NEW</div>
                    </div>
                    <div class="article-content">
                        <h3 class="article-title">{{ article.title }}</h3>
                        <p class="article-excerpt">{{ article.excerpt }}</p>
                        <div class="article-meta">
                            <span class="article-author">
                                <i class="el-icon-user"></i> {{ article.authorNickname || '未知作者' }}
                            </span>
                            <span class="article-date">
                                <i class="el-icon-date"></i>
                                {{ formatDate(article.publishTime) }}
                            </span>
                        </div>
                        <div class="article-tags">
                            <span
                                class="tag"
                                v-for="tag in article.tag.split(',')"
                                :key="tag"
                            >{{ tag }}</span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="pagination-container">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[10, 20, 30]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import axios from 'axios';

export default {
    name: 'ArticleSearch',
    data() {
        return {
            articles: [],
            total: 0,
            currentPage: 1,
            pageSize: 10,
            loading: false,
            cardScale: {}
        };
    },
    created() {
        this.fetchArticles();
    },
    watch: {
        '$route.query.searchDTO': {
            immediate: true,
            handler() {
                this.currentPage = 1;
                this.fetchArticles();
            }
        }
    },
    methods: {
        async fetchArticles() {
            this.loading = true;
            const searchDTO = this.$route.query.searchDTO ? JSON.parse(this.$route.query.searchDTO) : {};
            try {
                const response = await axios.get('/article/search', {
                    params: {
                        ...searchDTO,
                        pageNum: this.currentPage,
                        pageSize: this.pageSize
                    }
                });
                this.articles = response.data.data.records;
                this.total = response.data.data.total;
                // 获取每篇文章作者的昵称
                for (const article of this.articles) {
                    try {
                        const userResponse = await axios.get(`/user/info/${article.userId}`);
                        if (userResponse.data.code === 200) {
                            const user = userResponse.data.data;
                            article.authorNickname = user.nickname;
                        }
                    } catch (error) {
                        console.error(`获取文章 ${article.id} 作者信息失败`, error);
                    }
                }
                this.loading = false;
            } catch (error) {
                console.error('搜索文章出错:', error);
                this.loading = false;
            }
        },
        handleSizeChange(newSize) {
            this.pageSize = newSize;
            this.fetchArticles();
        },
        handleCurrentChange(newPage) {
            this.currentPage = newPage;
            this.fetchArticles();
        },
        scaleCard(id) {
            this.$set(this.cardScale, id, 1.03);
        },
        resetScale(id) {
            this.$set(this.cardScale, id, 1);
        },
        formatDate(timestamp) {
            return new Date(timestamp).toLocaleDateString();
        }
    }
};
</script>

<style scoped>
/* 全局样式 */
.article-search-container {
    display: grid;
    grid-template-columns: 1fr;
    gap: 50px;
    padding: 30px 40px;
    min-height: 100vh;
    background: transparent;
    position: relative;
    font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
}

/* 背景动态点缀 */
.bg-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    overflow: hidden;
}

.bg-dot {
    position: absolute;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #ffb76b, #ff7c00);
    opacity: 0.1;
    animation: float 6s infinite ease-in-out;
    filter: blur(2px);
}

.dot-2 {
    right: 10%;
    bottom: 15%;
    width: 80px;
    height: 80px;
    animation-delay: -2s;
    animation-duration: 8s;
}

.dot-3 {
    left: 20%;
    top: 70%;
    width: 100px;
    height: 100px;
    animation-delay: -3s;
    animation-duration: 10s;
}

@keyframes float {
    0%,
    100% {
        transform: translateY(0) rotate(0deg);
    }
    50% {
        transform: translateY(-30px) rotate(5deg);
    }
}

/* 标题样式 */
.section-title {
    font-size: 24px;
    font-weight: bold;
    color: #303133;
    margin-bottom: 30px;
    position: relative;
    padding-left: 15px;
}

.section-title::before {
    content: "";
    position: absolute;
    left: 0;
    top: 5px;
    height: 20px;
    width: 5px;
    background: #ffb76b;
    border-radius: 3px;
}

/* 文章卡片 */
.articles-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 30px;
}

.article-card {
    background: #fff;
    border-radius: 20px;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.08);
    overflow: hidden;
    transition: all 0.3s;
    transform: scale(var(--scale, 1));
    position: relative;
}

.article-card:hover {
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.12);
    transform: scale(var(--scale, 1.03));
}

.article-image-container {
    position: relative;
    overflow: hidden;
    height: 200px;
}

.article-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.5s;
}

.article-card:hover .article-image {
    transform: scale(1.05);
}

.article-badge {
    position: absolute;
    top: 15px;
    right: 15px;
    background: #ff6b6b;
    color: white;
    padding: 4px 10px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: bold;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.05);
    }
    100% {
        transform: scale(1);
    }
}

.article-content {
    padding: 20px;
}

.article-title {
    font-size: 18px;
    font-weight: bold;
    color: #303133;
    margin-bottom: 10px;
    line-height: 1.4;
}

.article-excerpt {
    font-size: 14px;
    color: #606266;
    margin-bottom: 15px;
    line-height: 1.6;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.article-meta {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: #909399;
    margin-bottom: 15px;
}

.article-meta i {
    margin-right: 5px;
}

.article-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.article-tags .tag {
    padding: 4px 10px;
    border-radius: 12px;
    background: rgba(255, 183, 107, 0.1);
    font-size: 12px;
    color: #ff7c00;
}

/* 分页控件 */
.pagination-container {
    margin-top: 40px;
    display: flex;
    justify-content: center;
}
</style>